//
//  main.m
//  Demo0020 
//
//  Created by zTercel on 13-1-30.
//  Copyright (c) 2013年 zTercel. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface NSSet(NSPrinting)
-(void) print;
@end

@implementation NSSet(NSPrinting)
-(void) print: (NSString*)prompt
{
    int index = 0;
    NSMutableString* content = [NSMutableString stringWithFormat:@"%@: \n", prompt];
    for (id value in self)
    {
        [content appendFormat: @"member: %i = %@\n", index++, value];
    }
    NSLog(@"%@  ", content);
    // NSEnumerator* enumerator = [self objectEnumerator];
    // id value;
    // index = 0;
    // while (value = [enumerator nextObject])
    // {
    //    NSLog(@"%i = %@", index++, value);
    // }
}
@end

@interface DemoSet: NSObject
-(void) create;
-(void) getSetCount;
-(void) accessSetObject;
-(void) compareSet;
@end

@implementation DemoSet
-(void) create
{
    //Initializing a Set
    //-----------------------------------------
    //– initWithArray:
    //– initWithObjects:
    //– initWithObjects:count:
    //– initWithSet:
    //– initWithSet:copyItems:
    NSArray* objArray = [NSArray arrayWithObjects:@"chen", @"xiao", @"ling", @"name", @"ztercel", nil];
    NSSet* set1 = [[NSSet alloc]initWithArray:objArray];
    [set1 print:@"set1 content"];
    
    NSSet* set7 = [NSSet setWithObjects: (const id*)&set1 count:1];  // 这个函数理解和实践有些不对，从帮助理解应该是把C数组中取count个数到set中， 但实际发现会从第一个set开始取后继count个set到新的set中？  
    [set7 print:@"set7 content"];
    
    NSSet* set2 = [NSSet setWithObject:@"boy"];
    [set2 print:@"set2 content"];
    
    NSSet* set3 = [NSSet setWithObjects:@"i", @"am", @"a", @"boy", nil];
    [set3 print:@"set3 content"];
    
    NSSet* set4 = [NSSet setWithSet:set3];
    [set4 print:@"set4 content"];
    
    NSSet* set5 = [[NSSet alloc]initWithSet: set3 copyItems: YES];
    [set5 print:@"set5 content"];
    
    NSSet* set6 = [set5 setByAddingObject:@"objective"];
    [set6 print:@"set6 content"];
}

-(void) getSetCount
{
    //Counting Entries
    //-----------------------------------------
    //– count
    NSSet* set = [NSSet setWithObjects:@"hello", @"objective", @"c", @".", nil];
    NSLog(@"%@ count: %lu", set, [set count]);
    
    return;
}

-(void) accessSetObject
{
    //Accessing Set Members
    //-----------------------------------------
    //– allObjects
    //– anyObject
    //– containsObject:
    //– filteredSetUsingPredicate:
    //– makeObjectsPerformSelector:
    //– makeObjectsPerformSelector:withObject:
    //– member:
    //– objectEnumerator
    //– enumerateObjectsUsingBlock:
    //– enumerateObjectsWithOptions:usingBlock:
    //– objectsPassingTest:
    //– objectsWithOptions:passingTest:
    NSSet* set = [NSSet setWithObjects:@"i", @"learning", @"objective-c", @"and", @"iphone",nil];
    NSLog(@"all objects(array): %@", [set allObjects]);
    
    NSLog(@"%@", [set anyObject]);
    NSLog(@"%@", [set containsObject:@"and"] ? @"contain" : @"no contain");
    NSString* string = @"learning";
    NSLog(@"%@", [set member:string]);
    NSEnumerator* enumerator = [set objectEnumerator];
    id value;
    while (value = [enumerator nextObject]) {
        NSLog(@"value: %@", value);
    }
    [set enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
        static int nIndex = 0;
        NSLog(@"processing count: %i", nIndex++);
        if ([obj isEqual:@"learning"])
        {
            NSLog(@"I found %@", obj);
            *stop = YES;
        }
    }];
}

-(void) compareSet
{
    //Comparing Sets
    //-----------------------------------------
    //– isSubsetOfSet:
    //– intersectsSet:
    //– isEqualToSet:
    //– valueForKey:
    //– setValue:forKey:
    NSSet* set = [NSSet setWithObjects:@"this", @"is", @"word", @"object", nil];
    NSSet* set1 = [NSSet setWithObjects:@"word", @"string", nil];
    NSLog(@"%@ isSubsetofSet %@ ? %s", set1, set, [set1 isSubsetOfSet:set] ? "yes" : "no");
    
    NSLog(@"%@ intersectsSet %@ ? %s", set1, set, [set1 intersectsSet:set] ? "yes" : "no");
    NSLog(@"%@ isEqualToSet %@ ? %s", set1, set, [set1 isEqualToSet:set] ? "yes" : "no");
}
@end


int main(int argc, const char * argv[])
{
    @autoreleasepool {
        DemoSet* demoSet = [[DemoSet alloc]init];
        [demoSet create];
        [demoSet getSetCount];
        [demoSet accessSetObject];
        [demoSet compareSet];
    }
    return 0;
}

